/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.shashank.platform.shotangapp.view;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Text;
import ohos.agp.text.Font;
import ohos.app.Context;
import ohos.app.Environment;
import ohos.global.resource.RawFileEntry;
import ohos.global.resource.Resource;
import ohos.global.resource.ResourceManager;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 自定义Font字体类
 */
public class FontTextView extends Text {
    private static final HiLogLabel TAG = new HiLogLabel(HiLog.LOG_APP, 0x000110, "FontTextView----");

    /**
     * 构造函数
     *
     * @param context 上下文
     */
    public FontTextView(Context context) {
        super(context);
        init(context);
    }

    /**
     * 广构造函数
     *
     * @param context context
     * @param attrSet attrSet
     */
    public FontTextView(Context context, AttrSet attrSet) {
        super(context, attrSet);
        init(context);
    }

    /**
     * 构造函数
     *
     * @param context   context
     * @param attrSet   attrSet
     * @param styleName styleName
     */
    public FontTextView(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        init(context);
    }

    private void init(Context context) {
        String raw = "resources/rawfile/";
        String ttf = "calibri.ttf";
        ResourceManager resManager = getContext().getResourceManager();
        RawFileEntry rawFileEntry = resManager.getRawFileEntry(String.format("%s%s", raw, ttf));
        File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), ttf);
        Resource resource = null;
        OutputStream outputStream = null;
        try {
            resource = rawFileEntry.openRawFile();
            outputStream = new FileOutputStream(file);
            int index;
            byte[] bytes = new byte[1024];
            while ((index = resource.read(bytes)) != -1) {
                outputStream.write(bytes, 0, index);
                outputStream.flush();
            }
        } catch (IOException e) {
            HiLog.info(TAG, "error : " + e.getMessage());
        } finally {
            try {
                if (resource != null) {
                    resource.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                HiLog.info(TAG, "error : " + e.getMessage());
            }
        }
        Font.Builder builder = new Font.Builder(file);
        Font font = builder.build();
        setFont(font);
    }
}